﻿using System;
using System.Reflection;


namespace VsTortoiseSVN
{
    /// <summary>
    /// Numerical Id's of the bitmap resources as they're found in the VsTortoise satellite DLL.
    /// Since these bitmaps are used for Commands/MSOButton glyphs, resource identifier have to be numerical values.
    /// See Commands2.AddNamedCommand2 in MSDN or http://msdn.microsoft.com/en-us/library/ms228771%28VS.80%29.aspx
    /// </summary>
    enum BitmapResourceId
    {
        None = 0,
        Update = 1000,
        Commit = 1001,
        Diff = 1002,
        DiffWithPreviousVersion = Diff,
        ShowLog = 1003,
        Blame = 1004,
        Add = 1005,
        Revert = 1006,
        OpenModifiedFile = 1007,
        Lock = None,
        Unlock = None,
        About = None,
        FindPreviousModification = 1008,
        FindNextModification = 1009,
        EditConflicts = 1010,
        Resolve = 1011,
        Properties = 1012,
        ExploreTo = 1013,
    }

    /// <summary>
    /// ResourceManager is the interface to the VsTortoise satellite DLL.
    /// </summary>
    public class SatelliteDLL
    {
        System.Resources.ResourceManager _textResources;
        System.Resources.ResourceManager _imageResources;

        #region Singleton
        private static SatelliteDLL _instance;
        public static SatelliteDLL Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new SatelliteDLL();

                return _instance;
            }
        }
        #endregion

        /// <summary>
        /// Constructor
        /// </summary>
        protected SatelliteDLL()
        {
            Assembly satelliteAssembly = null;
            System.Globalization.CultureInfo culture = null;
            try
            {
                // Try to retrieve the satellite assembly of the Visual Studio UI culture
                culture = System.Threading.Thread.CurrentThread.CurrentUICulture;
                satelliteAssembly = Assembly.GetExecutingAssembly().GetSatelliteAssembly(culture);
            }
            catch
            {
                // We don't seem to have a satellite assembly
                // for the current Visual Studio UI culture.
            }

            if (satelliteAssembly == null)
            {
                // Use the english satellite assembly instead
                culture = System.Globalization.CultureInfo.GetCultureInfo("en-US");
                satelliteAssembly = Assembly.GetExecutingAssembly().GetSatelliteAssembly(culture);
            }

            // Creates the resource manager that contains texts
            string textsName = string.Format("VsTortoise.Texts.{0}", culture.Name);
            _textResources = new System.Resources.ResourceManager(textsName, satelliteAssembly);

            // Creates the resource manager that contains images
            string imagesName = string.Format("VsTortoise.Images.{0}", culture.Name);
            _imageResources = new System.Resources.ResourceManager(imagesName, satelliteAssembly);
        }

        /// <summary>
        /// Gets whether a string with the specified id exists
        /// in the satellite resource dll string table.
        /// </summary>
        public bool HasText(string id)
        {
            string text = _textResources.GetString(id);
            return !string.IsNullOrEmpty(text);
        }

        /// <summary>
        /// Gets the text of the string represented by id.
        /// If the text for the id is not available, the method will
        /// return the id instead.
        /// </summary>
        public string GetText(string id)
        {
            string text = _textResources.GetString(id);
            if (string.IsNullOrEmpty(text))
                return "<" + id + ">";

            return text;
        }

        /// <summary>
        /// Gets the bitmap resource with the specified id or null when not available.
        /// </summary>
        public System.Drawing.Bitmap GetBitmap(int id)
        {
            System.Drawing.Bitmap bitmap = _imageResources.GetObject(id.ToString()) as System.Drawing.Bitmap;
            return bitmap;
        }

        /// <summary>
        /// Gets the bitmap resource with the specified id or null when not available.
        /// The bitmaps transparent color is set to the color of the pixel in the bottom left corner.
        /// </summary>
        public System.Drawing.Bitmap GetTransparentBitmap(int id)
        {
            System.Drawing.Bitmap bitmap = GetBitmap(id);
            if (bitmap != null)
            {
                System.Drawing.Color transparentColor = bitmap.GetPixel(0, bitmap.Height - 1);
                bitmap.MakeTransparent(transparentColor);
            }

            return bitmap;
        }
    }
}
